package com.example.network_sdk;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
import retrofit2.converter.gson.GsonConverterFactory;

public class RetrofitManager {
    private RetrofitManager(){

    }

    private volatile static RetrofitManager retrofitManager=new RetrofitManager();

    public synchronized static RetrofitManager getInstance(){
        return retrofitManager;
    }

    private Retrofit retrofit;

    public void create(){
        retrofit=new Retrofit.Builder()
                .client(new OkHttpClient().newBuilder()
                        .connectTimeout(3000, TimeUnit.MINUTES)
                        .writeTimeout(3000,TimeUnit.MINUTES)
                        .readTimeout(3000,TimeUnit.MINUTES)
                        .addInterceptor(new HttpLoggingInterceptor().setLevel(HttpLoggingInterceptor.Level.BODY))
                        .build())
                .addCallAdapterFactory(RxJava2CallAdapterFactory.create())
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl("http://www.qubaobei.com/")
                .build();
    }

    public Retrofit getRetrofit() {
        if (retrofit==null){
            create();
        }
        return retrofit;
    }
}
